package com.vcaresolution.logistic.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.DepartmentForm;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>DepartmentController</code> is used to track all Department related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class DepartmentController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.DEPARTMENTFORM_MODELATTRIBUTE)
	public DepartmentForm createForm(){
		return new DepartmentForm();
	}
	
	/**
	 * This method is called when the GET request has been made to the AddDepartment Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_DEPARTMENT_URL,method = RequestMethod.GET)
	public ModelAndView loadAddDepartment( @RequestParam(value="branchId") String branchId,
			@RequestParam(value="companyId") String companyId) {
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("branchId",branchId);
		modelAndView.addObject("companyId",companyId);
		modelAndView.setViewName(ApplicationConstants.ADD_DEPARTMENT_VIEW);
		modelAndView.addObject(ApplicationConstants.DEPARTMENTFORM_MODELATTRIBUTE, new DepartmentForm());
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the AddDepartment Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_DEPARTMENT_URL,method = RequestMethod.POST)
	public ModelAndView processAddDepartment(@Valid DepartmentForm departmentForm, final BindingResult result,final HttpServletRequest request,
											final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		if( !result.hasErrors() ){
			getServiceRegistry().getDepartmentService().add(departmentForm);
			
			redirectAttrs.addFlashAttribute(ApplicationConstants.SUCCESS_MESSAGE,getMessageSource().getMessage("departmentForm.added.message", null, null));
			modelAndView.setView(new RedirectView(ApplicationConstants.EDIT_BRANCH_URL+"?branchid="+departmentForm.getBranchId()
					+"&companyId="+departmentForm.getCompanyId(),true, true, false));
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the EditDepartment Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_DEPARTMENT_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditDepartment(@ModelAttribute(ApplicationConstants.DEPARTMENTFORM_MODELATTRIBUTE)
											final DepartmentForm DepartmentForm , @RequestParam(value="departmentid") String departmentid,
											@RequestParam(value="branchId") String branchId,
											@RequestParam(value="companyId") String companyId, final BindingResult result, 
											final Model model) {
		
		ModelAndView modelAndView = new ModelAndView();
		
		Object departmentObject = getServiceRegistry().getDepartmentService().findById(Integer.parseInt(departmentid));
		modelAndView.addObject("branchId",branchId);
		modelAndView.addObject("companyId",companyId);
		modelAndView.addObject(ApplicationConstants.DEPARTMENTFORM_MODELATTRIBUTE, departmentObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_DEPARTMENT_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the EditDepartment Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_DEPARTMENT_URL,method = RequestMethod.POST)
	public ModelAndView processEditDepartment(@Valid DepartmentForm departmentForm, final BindingResult result,final HttpServletRequest request,
											final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		if( !result.hasErrors() ){
			getServiceRegistry().getDepartmentService().update(departmentForm);
			
			redirectAttrs.addFlashAttribute(ApplicationConstants.SUCCESS_MESSAGE,getMessageSource().getMessage("departmentForm.updated.message", null, null));
			modelAndView.setView(new RedirectView(ApplicationConstants.EDIT_BRANCH_URL+"?branchid="+departmentForm.getBranchId()+"&companyId="
					+departmentForm.getCompanyId(),true, true, false));
		}
		
		return modelAndView;
	}
}
